<?
include_once('../inc/functions.php');
require_once('../inc/global.php');
require_once('inc/user_userdata.php');
        $IsAjax = isset($_REQUEST['IsAjax']);
        //print_r($_REQUEST);
        if(isset($_REQUEST['Save']))
        {
                $rs = $_REQUEST;
                $errors = Users::Validate($rs);
                if(!$errors)
                        $errors = Users::Save($rs);
                if(!$errors)
                {
                        if($IsAjax)
                        {
                                include 'inc/Users_det.php';
                                die();
                        }
                        else
                                header("Location: ../Users.php?new_id=$rs[id]");
                }
        }
        elseif(isset($_REQUEST['id']))
                $rs = Users::Get($_REQUEST['id']);
        else
                $rs = Users::Blank();
                
?>

<? if($IsAjax){ ?>
        <form method="post" action="edit.php?IsAjax=true<?=isset($_REQUEST['id']) ? "&id=$_REQUEST[id]" : ''?>">
                <input  type="text"
                        name="FirstName" id="FirstName"
                    value="<?=$rs['FirstName']?>"
                    class="<?=isset($errors['FirstName']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['FirstName']?></span>
                <input  type="text"
                        name="LastName" id="LastName"
                    value="<?=$rs['LastName']?>"
                    class="<?=isset($errors['LastName']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['LastName']?></span>
                <input  type="text"
                        name="PictureURL" id="PictureURL"
                    value="<?=$rs['PictureURL']?>"
                    class="<?=isset($errors['PictureURL']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['PictureURL']?></span>
                <input  type="text"
                        name="FBID" id="FBID"
                    value="<?=$rs['FBID']?>"
                    class="<?=isset($errors['FBID']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['FBID']?></span>
                <input  type="number"
                        name="Lookup_id" id="Lookup_id"
                    value="<?=$rs['Lookup_id']?>"
                    class="<?=isset($errors['Lookup_id']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['Lookup_id']?></span>
            <input type="hidden" value="true"  name="Save"/>
            <input type="submit" value="Save"/>
            <a href="#" class="cancel">Cancel</a>
       </form>

<? }else{ ?>
                <? 
	include('inc/header.php')
?>
<title>Users</title>
        <style type="text/css">
                        .error { color: red; }
                        input.error {
                                border: 1px solid red;
                                background: #FEE;
                        }
                        form {
                                width: 300px;
                                float: left;
                        }
                </style>
    </head>
    
    <body>
               <? 
	include('inc/adminnav.php')
?>
<div class="content">
        <h2>Edit a Contact Method</h2>
        <ul class="error">
                <? foreach($errors as $field => $error){ ?>
          <li><?=$field?>: <?=$error?></li>
            <? } ?>
        </ul>
        
        <form method="post" action="">
                <label>id:</label>
                <?=$rs['id']?>
            <br />
                <label for="FirstName">First Name</label>
                <input  type="text"
                        name="FirstName" id="FirstName"
                    value="<?=$rs['FirstName']?>"
                    class="<?=isset($errors['FirstName']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['FirstName']?></span>
            <br />
                <label for="LastName">Last Name</label>
                <input  type="text"
                        name="LastName" id="LastName"
                    value="<?=$rs['LastName']?>"
                    class="<?=isset($errors['LastName']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['LastName']?></span>
            <br />
                <label for="Password">Password</label>
                <input  type="text"
                        name="Password" id="Password"
                    value="<?=$rs['Password']?>"
                    class="<?=isset($errors['Password']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['Password']?></span>
            <br />
                <label for="Lookup_id">Lookup_id</label>
                <input  type="number"
                        name="Lookup_id" id="Lookup_id"
                    value="<?=$rs['Lookup_id']?>"
                    class="<?=isset($errors['Lookup_id']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['Lookup_id']?></span>
            <br />
                <label for="PictureURL">PictureURL</label>
                <input  type="text"
                        name="PictureURL" id="PictureURL"
                    value="<?=$rs['PictureURL']?>"
                    class="<?=isset($errors['PictureURL']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['PictureURL']?></span>
            <br />
                <label for="FBID">FBID</label>
                <input  type="text"
                        name="FBID" id="FBID"
                    value="<?=$rs['FBID']?>"
                    class="<?=isset($errors['FBID']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['FBID']?></span>
            <br />
            <input type="submit" value="Save"  name="Save"/>
        </form>
        <div style="float:left">
        <?
                        $_REQUEST['User_id'] = $rs['id'];
                        include 'ContactMethods/index.php';
                ?>
        </div>
        </div>
        </div>
        </body>
</html>
<? } ?>